package com.wtwd.campus.receiver;

import com.wtwd.campus.entity.MqDeviceUpLocation;
import com.wtwd.campus.entity.MqLocationData;
import com.wtwd.campus.service.InjectFeignService;
import com.wtwd.campus.utils.InjectService;
import com.wtwd.campus.utils.LocationUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ActiveLocationReceiver implements Receiver {
    private static final Logger logger = LoggerFactory.getLogger(ActiveLocationReceiver.class);

    @Override
    public String parseMessage(String data) {
        //*WT,800115470000876,V4,D1,170745,170747,A,2250.2266,N,11391.6154,E,0.11,356,460,11,0,1,30501,124968449,108,108,0,8,030621,FFFFDFFF
        String[] split = data.split(",");
        String imei = split[1];
        String messageType = split[2];
        String type = split[3];
        String commandNumber = split[4];
        String time = split[5];
        String flag = split[6];//有效定位A,无效定位V
        String latitude = split[7];
        String longitude = split[9];
        String speed = split[11];
        String orientation = split[12];
        int num = Integer.parseInt(split[16]);
        String battery = split[split.length - 3];
        String gpsNum = split[14];
        String deviceDate = split[split.length - 2];
        String deviceStatus = split[split.length - 1];
        String lbsInfo = "";
        int num2 = Integer.valueOf(split[16]);
        for (int i = 0; i < num2; i++) {
            if (!StringUtils.isBlank(lbsInfo)) {
                lbsInfo = lbsInfo.concat("|");
            }
            lbsInfo = lbsInfo.concat(split[17 + i * 3]).concat(",").concat(split[18 + i * 3]).concat(",").concat(split[19 + 3 * i]);
        }
        lbsInfo = split[13] + "," + transferToGaoDeMnc(split[14]) + "," + lbsInfo;
        logger.info("基站信息" + lbsInfo);
        // mq传输对象
        MqDeviceUpLocation mqDeviceUpLocation = new MqDeviceUpLocation();
        mqDeviceUpLocation.setBattery(Integer.parseInt(battery));
        mqDeviceUpLocation.setDeviceDate(deviceDate);
        mqDeviceUpLocation.setCommandNumber(commandNumber);
        mqDeviceUpLocation.setImei(imei);
        mqDeviceUpLocation.setDeviceStatus(deviceStatus);
        mqDeviceUpLocation.setDeviceTime(time);
        mqDeviceUpLocation.setLbsInfo(lbsInfo);
        MqLocationData mqLocationData = new MqLocationData();
        List<Double> list = LocationUtils.transiformLatAndLng(longitude, latitude);
        mqLocationData.setLatitude(list.get(1));
        mqLocationData.setLongitude(list.get(0));
        mqLocationData.setSpeed(Double.parseDouble(speed));
        mqLocationData.setOrientation(Integer.parseInt(orientation));
        mqLocationData.setGpsNum(Integer.parseInt(gpsNum));
        mqDeviceUpLocation.setMqLocationData(mqLocationData);
        // 远程调用
        InjectFeignService.getIrmFeignClientService().queryLocationResponse(mqDeviceUpLocation);
        return imei;
    }

    public static String transferToGaoDeMnc(String mnc) {
        String gaoDeMnc = "0";//0是中国移动，1是联通
        if (mnc.equals("01") || mnc.equals("06") || mnc.equals("09")) {
            gaoDeMnc = "1";
        }
        return gaoDeMnc;
    }
}
